package com.wenhai.spring.dependency.injection;

import com.wenhai.xiaomage.spring.ioc.overview.dependency.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.util.ClassUtils;

import javax.annotation.Resource;
import java.beans.Introspector;

/**
 * @Author:wenhai
 * @Description: 注解方式字段依赖注入
 * @Date:2020/3/10
 */
public class AnnotationDependencyFieldInjectionDemo {
    @Autowired
    private User user;
    @Resource
    private User user2;
    public static void main(String[] args) {
        AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext();
        cxt.register(AnnotationDependencyFieldInjectionDemo.class);
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(cxt);
        String xmlResourcePath = "classpath:/META-INF/dependency-lookup-context.xml";
        beanDefinitionReader.loadBeanDefinitions(xmlResourcePath);
        cxt.refresh();
        System.out.println(Introspector.decapitalize(ClassUtils.getShortName(AnnotationDependencyFieldInjectionDemo.class)));
        AnnotationDependencyFieldInjectionDemo demo = cxt.getBean(AnnotationDependencyFieldInjectionDemo.class);
        System.out.println(demo.user);
        cxt.close();
    }


//    @Bean
//    public UserHolder userHolder(User user) {
//        UserHolder userHolder = new UserHolder();
//        userHolder.setUser(user);
//        return userHolder;
//    }
}
